package com;

import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by bruce on 2016/5/22.
 */
public class CustomHashMap<K, V> extends java.util.HashMap<K, V> {

    private static final long serialVersionUID = 1864601112267440506L;

    // TODO 需要强化各个 get 接口的异常检查, 如 null

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static CustomHashMap convert(HashMap map){

        CustomHashMap result = new CustomHashMap();

        for(Object key : map.keySet()) {
            result.put(key, map.get(key));
        }

        return result;
    }

    public String getString(K key){
        Object object = get(key);
        if(object != null){
            return object.toString();
        }
        return null;
    }

    public String getString(K key, String defaultValue){
        Object object = get(key);
        if(object != null){
            return object.toString();
        }
        return defaultValue;
    }

    public Boolean getBoolean(K key){
        Object object = get(key);
        if(object != null){
            return (Boolean)object;
        }
        return false;
    }

    public int getInt(K key){
        Object object = get(key);
        if(object != null){
            return NumberUtils.toInt(object.toString());
        }
        return 0;
    }

    public Integer getInteger(K key){
        Object object = get(key);
        if(object != null){
            return NumberUtils.toInt(object.toString());
        }
        return null;
    }

    public BigInteger getBigInteger(K key){
        Object object = get(key);
        if(object != null){
            return BigInteger.valueOf(Integer.valueOf(object.toString()));
        }
        return BigInteger.ZERO;
    }

    public BigDecimal getBigDecimal(K key) {
        Object object = get(key);
        if(object != null){
            return BigDecimal.valueOf(Double.valueOf(object.toString()));
        }
        return BigDecimal.ZERO;
    }

    public Long getLong(K key){
        Object object = get(key);
        if(object != null){
            return Long.valueOf(object.toString());
        }
        return null;
    }

    public Float getFloat(K key){
        Object object = get(key);
        if(object != null){
            return Float.valueOf(object.toString());
        }
        return null;
    }

    public Double getDouble(K key){
        Object object = get(key);
        if(object != null){
            return Double.valueOf(object.toString());
        }
        return null;
    }

    public Date getDate(K key) {
        Object object = get(key);
        if(object != null){
            return (Date)object;
        }
        return null;
    }

    public Time getTime(K key) {
        Object object = get(key);
        if(object != null){
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            java.util.Date date  = null;
            try {
                date = sdf.parse(object.toString());
                return new Time(date.getTime());
            } catch (ParseException e) {
                return null;
            }
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public HashMap<Object, Object> getHashMap(K key) {
        Object object = get(key);
        if(object != null){
            return (HashMap<Object, Object>)object;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public CustomHashMap<Object, Object> getCustomHashMap(K key) {
        Object object = get(key);
        if(object != null){
            return (CustomHashMap<Object, Object>)object;
        }
        return null;
    }
}
